<?php

    $this->PhpExcel->Excel();
    $this->PhpExcel->titulo_reporte("PRODUCTOS", "LISTADO DE PRODUCTOS");
    $this->PhpExcel->titulo_hoja("Productos");

    $ext = ($formato == 1) ? 'xlsx' : 'ods';
    $nombre = "LISTADO_PRODUCTOS".date("d_m_Y");

    $this->PhpExcel->hoja->getDefaultStyle()->applyFromArray(array('font' => array('name' => 'arial', 'size' => 10)));
    $this->PhpExcel->hoja->getStyle('A1')->applyFromArray(array('font' => array('bold' => true, 'size' => 12, 'color' => array('argb' => '8B0000'))));
    $this->PhpExcel->hoja->getStyle('A2')->applyFromArray(array('font' => array('bold' => true, 'size' => 11)));
    $this->PhpExcel->hoja->getStyle('A3')->applyFromArray(array('font' => array('bold' => false, 'size' => 10)));
    $this->PhpExcel->hoja->getStyle('A4')->applyFromArray(array('font' => array('bold' => false, 'size' => 10)));

    $arrStyleTitle = array('fill' => array('type' => PHPExcel_Style_Fill::FILL_SOLID,
            'startcolor' => array('argb' => '81DAF5')),
        'font' => array('bold' => true,
            'color' => array('argb' => PHPExcel_Style_Color::COLOR_WHITE)),
        'alignment' => array('horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER));

    $arrStyleTitleuno = array('fill' => array('type' => PHPExcel_Style_Fill::FILL_SOLID,
            'startcolor' => array('argb' => 'EFFBFB')));

    $arrborderTitleuno = array('style' => PHPExcel_Style_Border::BORDER_DASHDOT, 'color' => array('argb' => '000000'));

    $this->PhpExcel->configura_celda('A', 10); //CODIGO
    $this->PhpExcel->configura_celda('B', 30); //NOMBRE

    $index_title = 6;

    $j = 0;
    $tit[] = array('CODIGO', 'STR', 'L');
    $tit[] = array('NOMBRE', 'STR', 'L');

    foreach ($tit as $columnTitle) {
        $this->PhpExcel->valor_celda($j, $index_title, $columnTitle);
        $oColumn = $this->PhpExcel->obtener_columna($j, $index_title);
        $oRow = $this->PhpExcel->obtener_fila($j, $index_title);
        $this->PhpExcel->hoja->getStyle($oColumn . $oRow)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $this->PhpExcel->hoja->getStyle($oColumn . $oRow)->applyFromArray($arrStyleTitle);
        $this->PhpExcel->hoja->getStyle($oColumn . $oRow)->getBorders()->getAllBorders()->applyFromArray($arrborderTitleuno);
        $j++;
    }
    
    $this->PhpExcel->hoja->setAutoFilter("A$index_title:{$oColumn}{$oRow}");

    $fila = 7;

    foreach ($data as $detalle) {
        $valores = array();
        $valores[] = array($detalle['Producto']['codigo'], 'STR', 'L');
        $valores[] = array($detalle['Producto']['descripcion'], 'STR', 'L');

        $columna = 0;

        foreach ($valores as $valor) {
            $this->PhpExcel->valor_celda($columna, $fila, $valor);
            $oColumn = $this->PhpExcel->obtener_columna($columna, $fila);
            $oRow = $this->PhpExcel->obtener_fila($columna, $fila);
            if ($fila % 2 == 0) {
                $this->PhpExcel->hoja->getStyle($oColumn . $oRow)->applyFromArray($arrStyleTitleuno);
            }
            $this->PhpExcel->hoja->getStyle($oColumn . $oRow)->getBorders()->getAllBorders()->applyFromArray($arrborderTitleuno);
            $columna++;
        }
        $fila++;
    }

    $this->PhpExcel->mostrar($nombre, $ext);